package cn.bossx.org.service.impl;

import cn.bossx.basic.exception.BusinessException;
import cn.bossx.basic.service.impl.BaseServiceImpl;
import cn.bossx.org.domain.Employee;
import cn.bossx.org.domain.Shop;
import cn.bossx.org.mapper.EmployeeMapper;
import cn.bossx.org.mapper.ShopMapper;
import cn.bossx.org.service.IEmployeeService;
import cn.bossx.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private IEmployeeService employeeService;


    @Override
    public void settlement(Shop shop) {
        //一:校验
        // 1.两次密码是否一致 不一致  抛错(业务异常)
        Employee admin = shop.getAdmin();

        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BusinessException("两次密码不一致,请重新输入!!");
        }
        // 2.校验账户是否被注册 校验账号  和 t_employee 表中的:username email  phone
        Employee tmp = employeeMapper.loadByUsername(admin.getUsername());
        if (tmp != null) {
            throw new BusinessException("账户已经注册,请直接登录! 如果忘记密码,请走找回密码流程!!");
        }
        //二:注册
        // 1.保存employee 返回自增ID employee_id
        //employeeMapper.save(admin);
        employeeService.add(admin);//对应的业务,交给对应的service自己做
        // 2.给店铺设置admin_id,并保存店铺 返回自增ID  shop_id
        shop.setAdmin(admin);
        shopMapper.save(shop);
        // 3.修改employee中的shop_id
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
